﻿using ML.Entity;
using ML.Utils;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ML.Web.Controllers
{
    /// <summary>
    /// 公共类控制器
    /// </summary>
    public class CommonController : Controller
    {
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string UploadImage(HttpPostedFileBase file)
        {
            string upload = Server.MapPath("~/Upload/Image");
            if (!Directory.Exists(upload))
            {
                Directory.CreateDirectory(upload);
            }
            //图片图片后缀，判断图片是否如何图片规则
            string extension = "." + file.FileName.Split('.')[1];
            if (!RegexUtil.Image(extension))
            {
                return "";
            }

            //上传单张图片
            var newFileNmae = $"{CommonUtil.ReadDateTime()}{extension}";
            string path = Path.Combine(upload, Path.GetFileName(newFileNmae));
            file.SaveAs(path);
            string path_url = $"{Request.Url.Scheme}://{Request.Url.Authority}/Upload/Image/{newFileNmae}";
            return path_url;
        }

        /// <summary>
        /// 获取所有用户分类
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetUserTypeAll()
        {
            IEnumerable<DropDownList> list =
                from n in typeof(UserTypeStatus).GetEnumList().OrderBy(c => c.Id)
                select new DropDownList()
                {
                    value = n.Id.ToString(),
                    name = n.Name
                };
            var result = new BaseResult<IEnumerable<DropDownList>>(200, list);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取用户分类
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetUserType()
        {
            IEnumerable<DropDownList> list =
                from n in typeof(UserTypeStatus).GetEnumList().OrderBy(c => c.Id).Where(c => c.Id != 1)
                select new DropDownList()
                {
                    value = n.Id.ToString(),
                    name = n.Name
                };
            var result = new BaseResult<IEnumerable<DropDownList>>(200, list);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取所有的订单状态
        /// </summary>
        /// <returns></returns>
        public ActionResult GetOrderState()
        {
            IEnumerable<DropDownList> list =
                from n in typeof(OrderStatus).GetEnumList().OrderBy(c => c.Id)
                select new DropDownList()
                {
                    value = n.Id.ToString(),
                    name = n.Name
                };
            var result = new BaseResult<IEnumerable<DropDownList>>(200, list);
            return Json(result, JsonRequestBehavior.AllowGet);
        }
    }
}